package guoanmaker.personal.business.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import guoanmaker.personal.business.model.PersonalAppraise;
import guoanmaker.personal.business.model.PersonalUserLandLog;
import guoanmaker.personal.business.modelnobind.Address;
import guoanmaker.personal.business.modelnobind.PersonalInvoiceForUser;
import guoanmaker.personal.business.modelnobind.PersonalUserForUser;

@Repository
public class PersonalUserDao {
	
	@PersistenceContext
	EntityManager em;
	
	/**
	 * 
	* @Description: 通过id获取用户 
	* @param @param id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月12日 上午10:35:32     
	* @return PersonalUserForUser    
	* @throws 
	 */
	public PersonalUserForUser findById(String id){
		PersonalUserForUser user=null;
		String jpql="select new guoanmaker.personal.business.modelnobind.PersonalUserForUser(u.id,u.username,"
				+ "u.password,u.createTime,u.nickname,u.birthday,u.name,u.email,u.remark,u.portrait,"
				+ "u.portraitName,u.isProvider,u.weibouuid,u.wechatopenid,u.qqopenid,u.wechatnickname,"
				+ "u.qqnickname,u.weibonickname,u.wechatportrait,u.qqportrait,u.weiboportrait,u.wechatportraitN,"
				+ "u.qqportraitN,u.weiboportraitN,u.thirdtype,u.wechat,u.qq,u.weibo,u.jpushid,u.jpushAlias,u.company,u.department,u.stage,u.bent) from PersonalUser u where u.id='"+id+"'";
		Query query = em.createQuery(jpql);
		List<PersonalUserForUser> list=query.getResultList();
		if(list!=null&&list.size()>0){
			String jpql1="select new guoanmaker.personal.business.modelnobind.PersonalInvoiceForUser(i.invoiceName,i.taxNumber,i.invoiceType,"
					+ "i.telephone,i.address,i.bankName,i.bankNumber,i.businessLicensePic,i.businessPic,i.isdefault,i.id) from PersonalInvoice i left join i.user u where u.id='"+id+"'";
			Query query1 = em.createQuery(jpql1);
			List<PersonalInvoiceForUser> listI =query1.getResultList();
			user=list.get(0);
			user.setInvoices(listI);
			String jpql2="select new guoanmaker.personal.business.modelnobind.Address(a.name,a.address,a.telephone,"
					+ "a.isdefault,a.id) from PersonalAddress a left join a.user u where u.id='"+id+"'";
			Query query2 = em.createQuery(jpql2);
			List<Address> listA =query2.getResultList();
			user.setAddresses(listA);
		}
		return user;
	}
	
	/**
	 * 
	* @Description: 查询当天是否存在某用户特定方式登陆的记录 
	* @param username 用户名
	* @param landType 登陆方式
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年9月8日 上午10:08:54     
	* @return PersonalUserLandLog    
	* @throws 
	 */
	public PersonalUserLandLog findByLandLog(String username,String landType){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String now=sdf.format(new Date());
		String timeBegin=now+" 00:00:00";
		String timeEnd=now+" 23:59:59";
		String jpql="select l from PersonalUserLandLog l where l.username='"+username+"' and l.landType='"+landType+"' and l.landTime>='"+timeBegin+"' and l.landTime<='"+timeEnd+"'";
		Query query = em.createQuery(jpql);
		List<PersonalUserLandLog> list=query.getResultList();
		PersonalUserLandLog userLandLog=null;
		if(list!=null&&list.size()>0){
			userLandLog=list.get(0);
		}
		return userLandLog;
	}
	
	/**
	 * 
	* @Description: 查询当天生成图片验证码次数
	* @param @param telephone
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年9月12日 下午2:27:25     
	* @return Long    
	* @throws 
	 */
	public Long getDayCodes(String telephone){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String now=sdf.format(new Date());
		String timeBegin=now+" 00:00:00";
		String timeEnd=now+" 23:59:59";
		String jpql="select count(c) from PersonalPicCode c where c.telephone='"+telephone+"' and c.createTime>='"+timeBegin+"' and c.createTime<='"+timeEnd+"'";
		Query query = em.createQuery(jpql);
		Long dayCodes=(Long)query.getResultList().get(0);
		return dayCodes;
	}

}
